﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Listener.Model;
using Yipusoft.Account.Model;

public partial class BasicBase_Listenner_ListenEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            IsTrue();
        }
    }
    private void Bind()
    {
        if (Request["ID"] == null)
            return;

        CallUser  info=Yipusoft.Listener.BLL.CallUser.GetModel(int.Parse(Request["ID"]));
        if (info == null)
            return;

        this.lblLoginID.Text = info.LoginID.ToString();
        this.lblUserName.Text = info.UserName.ToString();

        string Dept = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Dept == null || Dept.Length < 5)
            return;

        string strSql = "";
        if (Dept.Substring(0, 4).Equals("1300"))
            strSql += " 1=1";
        else
            strSql += " Left(DepartmentID,4)='" + Dept.Substring(0, 4) + "'";

        this.DptDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(strSql);
        this.DptDepartment.DataTextField = "FullName";
        this.DptDepartment.DataValueField = "DepartmentID";
        this.DptDepartment.DataBind();
        this.DptDepartment.SelectedValue = info.DepartmentID;
        
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        lblPromt.Text = "";
        CallUser info = Yipusoft.Listener.BLL.CallUser.GetModel(int.Parse(Request["ID"]));
        if (info == null)
        {
            lblPromt.Text = "此用户不存在";
            return;
        }

        if (!info.Password.Trim().Equals(txtOldPwd.Text.Trim()))
        {
            lblPromt.Text = "您输入的旧密码不正确，请重新输入";
            return;
        }

        info.Password = txtPwd2.Text.Trim();
        info.DepartmentID = DptDepartment.SelectedValue;
        info.DepartmentName = DptDepartment.SelectedItem.Text;

        if (Yipusoft.Listener.BLL.CallUser.Update(info) > 0)
        {
            lblPromt.Text = "更新成功";
        }

    }
    protected void btnNewPwd_Click(object sender, EventArgs e)
    {
        lblPromt.Text = "";
        CallUser info = Yipusoft.Listener.BLL.CallUser.GetModel(int.Parse(Request["ID"]));
        if (info == null)
        {
            lblPromt.Text = "此用户不存在";
            return;
        }
        info.Password = "119119";    

        if (Yipusoft.Listener.BLL.CallUser.Update(info) > 0)
        {
            lblPromt.Text = "更新成功";
        }
    }

    /// <summary>
    /// 查看当前用用户是否是支队管理员
    /// </summary>
    /// <returns></returns>
    public void IsTrue()
    {
        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);
        if (list.Where(o => o.RoleID == "100200").Count() > 0)
        {
            btnNewPwd.Visible = true;
            lblText.Visible = true;
        }
        else
        {
            btnNewPwd.Visible = false;
            lblText.Visible = false;

        }

    }
}
